/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsServer;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

/**
 *
 * @author Peng Cui
 */
public class WebServer 
{
    public static void main(String[] args)
    {
        try
        {
            HttpsServer hs = HttpsServer.create(new InetSocketAddress(80),0);
            SSLContext sslContext = SSLContext.getInstance ( "SSL" );
            
            

 
            // setup the key manager factory
            KeyManagerFactory kmf = KeyManagerFactory.getInstance ( "SunX509" );

 
            // setup the trust manager factory
            TrustManagerFactory tmf = TrustManagerFactory.getInstance ( "SunX509" );

            
            
            
            
            
            
            
            
            
            
            hs.createContext("/httpRpc/getreport", new MyHandler());
            hs.createContext("/httpRpc/gettaskstatus", new Handler1());
            hs.createContext("/httpRpc/newtask", new Handler2());
            
            hs.setExecutor(null);
            hs.start();
            System.out.println("started");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }                             
    }    
}

class Handler1 implements HttpHandler
{
     public void handle(HttpExchange t) throws IOException
    {
        String hc = t.getHttpContext().getPath();
        
        String backMessage = "0:4"+"\n"+" ";        
        
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println("\n path is: " + hc + "\n Run at time: " + df.format(new Date()));
        
        t.sendResponseHeaders(200, backMessage.getBytes().length);
        OutputStream os = t.getResponseBody();
        os.write(backMessage.getBytes());
        os.close();
    }
}

class Handler2 implements HttpHandler
{
     public void handle(HttpExchange t) throws IOException
    {
        String hc = t.getHttpContext().getPath();
        
        String backMessage = "123";        
        
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println("\n start task " + hc + "\n Run at time: " + df.format(new Date()));
        
        t.sendResponseHeaders(200, backMessage.getBytes().length);
        OutputStream os = t.getResponseBody();
        os.write(backMessage.getBytes());
        os.close();
    }
}

class MyHandler implements HttpHandler
{
    public void handle(HttpExchange t) throws IOException
    {
        String hc = t.getHttpContext().getPath();

        ReadFile rf = new ReadFile();    
        StringBuilder fileText = new StringBuilder();
        try
        {
            fileText = rf.readFile("C:\\Documents and Settings\\Peng Cui\\桌面\\getreport");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println("\n path is: " + hc + "\n Run at time: " + df.format(new Date()));
        
        t.sendResponseHeaders(200, fileText.toString().getBytes().length);
        OutputStream os = t.getResponseBody();
        os.write(fileText.toString().getBytes());
        os.close();
    }
}  

class ReadFile 
{
    public StringBuilder readFile(String fileAddress) throws Exception
    {
        BufferedReader in = new BufferedReader(new FileReader(fileAddress));
        String line = null;
        StringBuilder rtn = new StringBuilder();
        while((line = in.readLine())!=null)
        {
            rtn.append(line+"\n");
        }
        in.close();
        
        return rtn;
    }
}